package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scaffold.authority.entity.Role;

import wangjg.commons.page.Page;

@Repository
public class RoleDAO extends BasicBizDAO<Role,Long>{

	public List<Role> pageQuery(Page page ) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Role> pageQuery(Page page, Integer type, String name, String intro, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if(type != null){
			criteria.add(Restrictions.eq("type", type));
		}
		if(name != null){
			criteria.add(Restrictions.eq("name", name));
		}
		if(intro != null){
			criteria.add(Restrictions.eq("intro", intro));
		}
		if(keyword!=null && !keyword.isEmpty()){
			LogicalExpression expr = Restrictions.or(
			Restrictions.like("name", keyword, MatchMode.ANYWHERE)
			, Restrictions.like("intro", keyword, MatchMode.ANYWHERE));
			criteria.add(expr);
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}



}
